package com.entor.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.entor.model.Files;
import com.entor.model.MeetingEntity;
import com.entor.model.MeetingMemberEntity;
import com.entor.model.Menber;
import com.entor.model.Msg;
import com.entor.service.BaseServiceI;
import com.entor.service.MeetingService;
import com.entor.util.FileIOUtil;

import jdk.nashorn.internal.runtime.Undefined;
@Controller
@RequestMapping("meetingC")
public class MeetingController extends BaseController<MeetingEntity>{
	
	@Autowired
	private MeetingService meetingSI;
	@Resource(name="filesService")
	private BaseServiceI<Files> fsi;
	@Override
	public List<MeetingEntity> find(MeetingEntity t) {
		List<MeetingEntity> list = meetingSI.find(t);
		return list;
	}
	
	@RequestMapping("/testTT")
	@ResponseBody
	public Msg testTT(Boolean b,Integer id){
		Msg msg = new Msg("测试",false,null);
		
		return msg;
	}
	
	@Override
	public Msg insert(MeetingEntity t,
			@RequestParam(required=false,value="file") MultipartFile[] files) {
		System.out.println(t.toString());
		return meetingSI.insert(t);
	}
	
	
	@RequestMapping("/index")
	public String meetingIndex(){
		return "/meeting/index";
	}
	
	@RequestMapping("/left")
	public String meetingFeft(){
		return "/meeting/left";
	}
	@RequestMapping("/top")
	public String meetingTop(){
		return "/meeting/top";
	}
	@RequestMapping("/center")
	public String meetingCenter(){
		return "/meeting/center";
	}
	
	@RequestMapping("/administer")
	public String selectAdminister(HttpServletRequest request){
		request.setAttribute("flat", "all");
		return "/meeting/menu/administer";
	}
	
	@RequestMapping("/aoe")
	public String addOrEdit(){
		
		return "/meeting/menu/add";
	}
	
	@RequestMapping("/editMeeting")
	@ResponseBody
	public Msg editMeeting(MeetingEntity entity,
						HttpSession session){
		Msg msg = new Msg();
		if(entity.getId()!=null){
			msg = meetingSI.edit(entity);
		}else{
			int mid = ((Menber)session.getAttribute("loginMem")).getId();
			entity.setMid(mid);
			msg = meetingSI.insert(entity);
		}
		return msg;
	}
	
	@RequestMapping("/fileHtml")
	public String fileHtml(){
		return "/meeting/menu/addFiles";
	}
	
	@RequestMapping("/join")
	@ResponseBody
	public Msg joinMeeting(String datas,
			HttpSession session){
		Msg msg = new Msg();
		MeetingEntity entity = JSON.parseObject(datas,MeetingEntity.class);
		int memberId = ((Menber)session.getAttribute("loginMem")).getId();
		msg = meetingSI.joinMeeting(entity, memberId);
		return msg;
	}
	
	@RequestMapping("/findMine")
	@ResponseBody
	public List<MeetingEntity> fineMine(HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session = request.getSession();
		int mId = ((Menber)session.getAttribute("loginMem")).getId();
		MeetingEntity entity = new MeetingEntity();
		entity.setMid(mId);
		List<MeetingEntity> list = meetingSI.find(entity);
		request.setAttribute("flat", "mine");
		return list;
	}
	
	@RequestMapping("/find2")
	@ResponseBody
	public List<MeetingEntity> findJoin(MeetingEntity entity,
			HttpServletRequest request){
		List<MeetingEntity> list = meetingSI.find(entity);
		HttpSession session = request.getSession();
		
//		int memberId = Integer.parseInt((String)session.getAttribute("id"));
		int memberId=((Menber)session.getAttribute("loginMem")).getId();
		List<MeetingMemberEntity> mmes = meetingSI.findJoin(null, memberId);
		Menber menber = new Menber();
		for (MeetingEntity me : list) {
			for (MeetingMemberEntity mme : mmes) {
				if(me.getId().equals(mme.getMeetingId())){
					menber.setId(mme.getMenberId());
					List<Menber> temp = new ArrayList<Menber>();
					temp.add(menber);
					me.setMenber(temp);
				}
			}
		}
		return list;
	}
	
	@RequestMapping("/addFiles")
	@ResponseBody
	public Msg addFiles(HttpSession session,
						@RequestParam(required=false,value="fs") MultipartFile[] files){
		Msg msg = new Msg();
		
		int userId = ((Menber)session.getAttribute("loginMem")).getId();
		try {
			List<Files> list = FileIOUtil.saveFile(files, "t_coc_meeting", userId);
			if(list!=null){
				msg = fsi.insert(list.get(0));
			}
		} catch (Exception e) {e.printStackTrace();}
		return msg;
	}
	
}
